/**
 * 
 */
package com.vector.ding.commons.util.file;

import java.io.File;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;

/**
 * 压缩工具类
 * @author vector
 *
 */
public class ZipUtil {

	private ZipUtil() {};
	
	/**
	 * 压缩文件，如果待压缩文件不存在，会抛出运行时异常
	 * @param srcFile		待压缩文件（【文件|文件夹】 对象）
	 * @param targerZipFile	目标压缩文件，如果文件存在，则覆盖
	 */
	public static void zip(File srcFile, File targerZipFile) {
		zip(srcFile, targerZipFile, true);
	}
	
	/**
	 * 压缩文件
	 * @param srcFile
	 * @param targerZipFile	目标压缩文件对象
	 * @param overwrite	是否覆盖已存在的文件
	 */
	public static void zip(File srcFile, File targerZipFile, boolean overwrite) {
		if (!srcFile.exists())
			throw new RuntimeException("src file [" 
					+ srcFile.getPath() + srcFile.getName()
					+ "] not exist!");
		
		if (targerZipFile.exists()) {
			if (overwrite) {
				targerZipFile.delete();
			} else {
				return ;
			}
		}
		
		zipByAnt(srcFile, targerZipFile);
	}
	
	/**
	 * ant压缩操作
	 * @param srcFile
	 * @param targerZipFile
	 */
	private static void zipByAnt(File srcFile, File targerZipFile) {
		Project project = new Project();
		Zip zip = new Zip();
		zip.setProject(project);
		zip.setDestFile(targerZipFile);
		FileSet fileSet = new FileSet();
		fileSet.setProject(project);
		if (srcFile.isDirectory())
			fileSet.setDir(srcFile);
		else 
			fileSet.setFile(srcFile);
		zip.addFileset(fileSet);
		zip.execute();
	}
	
	/**
	 * 解压文件，将压缩文件$zipFile$, 解压到文件目录$destFolder$下
	 * 默认会覆盖输出文件夹下的同名文件
	 * @param zipFile
	 * @param destFolder
	 * @see #unZip(File, File, boolean)
	 */
	public static void unZip(File zipFile, File destFolder) {
		unZip(zipFile, destFolder, true);
	}
	
	/**
	 * 解压文件，将压缩文件$zipFile$, 解压到文件目录$destFolder$下，
	 * @param zipFile
	 * @param destFolder
	 * @param overwrite
	 */
	public static void unZip(File zipFile, File destFolder, boolean overwrite) {
		if (!zipFile.exists())
			throw new RuntimeException("zip file [" 
					+ zipFile.getPath() + zipFile.getName()
					+ "] not exist!");
		
		if (destFolder.exists()) {
			if (destFolder.isFile()) {
				throw new RuntimeException("dest file [" 
						+ destFolder.getPath() + destFolder.getName()
						+ "] is not a directory!");
			}
			/*if (covertOutputFolder) {
				try {
					FileUtils.cleanDirectory(destFolder);;
				} catch (IOException e) {
					throw new RuntimeException("clean directory [" 
							+ destFolder.getPath() + destFolder.getName()
							+ "] error:" + e.getMessage());
				}
			}*/
		} else {
			destFolder.mkdirs();
		}
		
		unZipByAnt(zipFile, destFolder, overwrite);
	}
	
	private static void unZipByAnt(File zipFile, File destFolder, boolean overwrite) {
		Project project = new Project();
		Expand expand = new Expand();
		expand.setProject(project);
		expand.setSrc(zipFile);
		expand.setDest(destFolder);
		expand.setOverwrite(overwrite);
		expand.execute();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	}

}
